#include "mainwindow.h"
#include "dieselgenerator.h"
#include "pvsystem.h"
#include <QListView>
#include <QStandardItemModel>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    // 设置窗口属性
    this->setWindowTitle("微电网能源管理系统");
    this->resize(800, 600);

    // 创建中央部件和主布局
    QWidget *centralWidget = new QWidget(this);
    QVBoxLayout *mainLayout = new QVBoxLayout(centralWidget);
    this->setCentralWidget(centralWidget);

    // 创建按钮布局
    QHBoxLayout *buttonLayout = new QHBoxLayout();
    m_btnAddDiesel = new QPushButton("添加柴油发电机");
    m_btnAddPV = new QPushButton("添加光伏系统");
    m_btnRunSimulation = new QPushButton("运行仿真");

    buttonLayout->addWidget(m_btnAddDiesel);
    buttonLayout->addWidget(m_btnAddPV);
    buttonLayout->addWidget(m_btnRunSimulation);

    // 创建文本输出区域
    m_textOutput = new QTextEdit();
    m_textOutput->setReadOnly(true);

    // 组装界面
    mainLayout->addLayout(buttonLayout);
    mainLayout->addWidget(m_textOutput);

    // 连接信号和槽
    connect(m_btnAddDiesel, &QPushButton::clicked, this, &MainWindow::onAddDiesel);
    connect(m_btnAddPV, &QPushButton::clicked, this, &MainWindow::onAddPV);
    connect(m_btnRunSimulation, &QPushButton::clicked, this, &MainWindow::onRunSimulation);
}

MainWindow::~MainWindow() {
    // 析构函数 - Qt会自动删除子控件，无需手动删除
}

void MainWindow::onAddDiesel() {
    static int count = 1;
    QString name = QString("柴油发电机%1").arg(count++);
    m_microgridSystem.addComponent(new DieselGenerator(name, 200, 50, 0.35));
    m_textOutput->append("已添加柴油发电机: " + name);
}

void MainWindow::onAddPV() {
    static int count = 1;
    QString name = QString("光伏系统%1").arg(count++);
    m_microgridSystem.addComponent(new PVSystem(name));
    m_textOutput->append("已添加光伏系统: " + name);
}

void MainWindow::onRunSimulation() {
    m_textOutput->append("开始运行仿真...");
    m_microgridSystem.runSimulation();
    m_textOutput->append("仿真完成!");
}
